package com.cuit.qx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.qx.bean.Warning;
import com.cuit.qx.bean.WarningWindExport;
import com.cuit.qx.dao.WarningMapper;
import com.cuit.qx.service.WarningService;
import com.cuit.qx.utils.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author:wjd
 * @Description:
 * @date:Created in 15:01 2020/9/21
 * @Modified By:
 */
@Service
public class WarningServiceImpl extends ServiceImpl<WarningMapper, Warning> implements WarningService {

    @Autowired
    WarningMapper mapper;

    /**
     * @return
     * @Author:wjd on 2020/9/21 15:35
     * @param:
     * @Description: 查询未点击确认的报警数据 注意：需要与前端保持长链接
     */
    @Override
    public List<Warning> getWarningByState() {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_state", 1);
        return mapper.selectList(queryWrapper);
    }

    /**
     * @return
     * @Author:wjd on 2020/9/21 15:32
     * @param:
     * @Description: 统计未点击确认的条数   注意：需要与前端保持长链接
     */
    @Override
    public long getWarningCount() {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_state", 1);
        return mapper.selectList(queryWrapper).stream().count();
    }

    /**
     * @return
     * @Author:wjd on 2020/9/21 15:43
     * @param:
     * @Description: 根据ID把状态为1的修改为0
     */
    @Override
    public int updateWarningById(String stationId) {
        int num = mapper.updateStateById(stationId);
        // 用户点击预警确认后,再次推送预警内容到轮播图
        if (num > 0) {
            WebSocket webSocket = new WebSocket();
            webSocket.sendAllMessage(null);
        }
        return num;
    }

    /**
     * 获取预警信息状态为1的对象
     *
     * @return
     */
    @Override
    public List<Warning> getWarnings() {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_state", 1);
        List<Warning> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 获取预警信息状态为1的条数
     *
     * @return
     */
    @Override
    public long WarningCount() {
        long count = getWarnings().stream().count();
        return count;
    }

    /**
     * 获取预警信息状态为1,当天时间的对象
     *
     * @return
     */
    @Override
    public List<Warning> getTodayWarnings() {
        // 设置时间格式,获取当前时间 yyyy-MM-dd HH
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        String todayTime = simpleDateFormat.format(zero);

        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_state", 1).ge("create_time", todayTime);
        List<Warning> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public IPage<Warning> getWarningList(Integer pageNo, Integer pageSize, String warningState) {
        Page<Warning> page = new Page<Warning>(pageNo, pageSize);
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_state", warningState).orderByDesc("create_time");
        IPage<Warning> warningIPage = mapper.selectPage(page, queryWrapper);
        List<Warning> warningList = warningIPage.getRecords();
        for (Warning warning : warningList) {
            if (warning.getElementType().equals("wind") && warning.getElementValue() != null && Integer.parseInt(warning.getElementValue()) >= 7) {
                warning.setWindStatus(1);
            } else {
                warning.setWindStatus(0);
            }
        }
        return warningIPage;
    }

    @Override
    public List<String> getWarningListContent() {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_state", 1);
        List<Warning> list = this.list(queryWrapper);
        List<String> warningContents = new ArrayList<>();
        for (Warning warning : list) {
            String warningContent = warning.getWarningContent();
            warningContents.add(warningContent);
        }
        return warningContents;
    }

    @Override
    public String getWarningLastTime() {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("warning_state", 1);
        List<Warning> list = this.list(queryWrapper);
        String createTime = "";
        if (list.size()>0) {
            createTime = list.get(0).getCreateTime();
            return createTime;
        } else {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            createTime = df.format(new Date());
            return createTime;
        }
    }

    @Override
    public int insertWarningData(Warning warning) {
        return mapper.insert(warning);
    }

    @Override
    public IPage<Warning> getQueryWarningList(Integer pageNo, Integer pageSize, String stationName, String stationNumber, String address, String warningState, String startTime, String endTime) {
        Page<Warning> page = new Page<Warning>(pageNo, pageSize);
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        if (stationName != null && !("".equals(stationName))) {
            queryWrapper.like("station_name", stationName);
        }
        if (stationNumber != null && !("".equals(stationNumber))) {
            queryWrapper.like("station_number", stationNumber);
        }
        if (address != null && !("".equals(address))) {
            queryWrapper.like("station_site", address);
        }
        if (warningState != null && !("".equals(warningState))) {
            int i = Integer.parseInt(warningState);
            queryWrapper.eq("warning_state", i);
        }
        if (startTime != null && !("".equals(startTime))) {
            queryWrapper.ge("create_time", startTime);
        }
        if (endTime != null && !("".equals(endTime))) {
            queryWrapper.le("create_time", endTime);
        }
        IPage<Warning> warningIPage = mapper.selectPage(page, queryWrapper);
        List<Warning> warningList = warningIPage.getRecords();
        for (Warning warning : warningList) {
            if (warning.getElementType().equals("wind") && warning.getElementValue() != null && Integer.parseInt(warning.getElementValue()) >= 7) {
                warning.setWindStatus(1);
            } else {
                warning.setWindStatus(0);
            }
        }
        return warningIPage;
    }


    @Override
    public List<Warning> getWarning(String startTime, String endTime) {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        if (startTime != null && !("".equals(startTime))) {
            queryWrapper.ge("create_time", startTime);
        }
        if (endTime != null && !("".equals(endTime))) {
            queryWrapper.le("create_time", endTime);
        }
        List<Warning> warnings = mapper.selectList(queryWrapper);
        return warnings;
    }

    @Override
    public int getWarningNum(String stationNumber, String startTime, String endTime) {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_number", stationNumber);
        queryWrapper.ge("create_time", startTime);
        queryWrapper.le("create_time", endTime);
        Integer num = mapper.selectCount(queryWrapper);
        return num;
    }

    @Override
    public List<WarningWindExport> selectWind(String startTime, String endTime, String addrCounty, String orderType) {
        List<Map> warningList = mapper.selectWind(startTime, endTime, addrCounty);
        List<WarningWindExport> warningwindList = new ArrayList<>();
        for (Map map1 : warningList) {
            WarningWindExport warningwind = new WarningWindExport();
            warningwind.setStationId(map1.get("station_id").toString());
            warningwind.setStationNumber(map1.get("station_number").toString());
            warningwind.setStationName(map1.get("station_name").toString());
            warningwind.setStationCounty(map1.get("station_county").toString());
            warningwind.setStationSite(map1.get("station_site").toString());
            warningwind.setWarningContent(map1.get("warning_content").toString());
            warningwind.setWarningState(Integer.valueOf(map1.get("warning_state").toString()));
            warningwind.setCreateTime(map1.get("create_time").toString());
            warningwind.setWindScale(Integer.parseInt(map1.get("element_value").toString()));
            String warningContent = map1.get("warning_content").toString();
            String str1 = warningContent.substring(warningContent.indexOf("到") + 1);
            String str2 = str1.substring(0, str1.indexOf("m/s"));
            warningwind.setWind(Double.valueOf(str2));
            warningwindList.add(warningwind);
        }

        if (orderType.equals("wind")) {
            Collections.sort(warningwindList, new Comparator<WarningWindExport>() {
                @Override
                public int compare(WarningWindExport o1, WarningWindExport o2) {
                    if (o1.getWind() > o2.getWind()) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            });
        }
        return warningwindList;
    }

    @Override
    public int deleteWarningByIds(String ids) {
        String[] id = ids.split(",");
        return mapper.deleteIds(Arrays.asList(id));
    }

    @Override
    public int deleteWarningByTime(String startTime, String endTime) {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_state", 0);
        queryWrapper.ge("create_time", startTime);
        queryWrapper.le("create_time", endTime);
        return mapper.delete(queryWrapper);
    }

    /**
     * 未确认预警数据
     *
     * @return
     */
    @Override
    public List<Warning> getWarningListWithState() {
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("warning_state", 1);
        List<Warning> list = mapper.selectList(queryWrapper);
        return list;
    }

    //批量入库
    @Override
    public Boolean insertBatch(List<Warning> warnings) {
        return mapper.insertBatch(warnings);
    }

}
